package cn.com.zero.front.auth.service.impl;

import cn.com.zero.base.exception.ErrorCodeException;
import cn.com.zero.base.service.BaseService;
import cn.com.zero.base.util.SensitiveInfoUtil;
import cn.com.zero.common.auth.security.po.AuthUserPO;
import cn.com.zero.file.service.IFileService;
import cn.com.zero.file.vo.FileInfo;
import cn.com.zero.front.auth.constant.FrontAuthConstants;
import cn.com.zero.front.auth.mapper.IUserMapper;
import cn.com.zero.front.auth.po.AuthUserInfoPO;
import cn.com.zero.front.auth.po.UserInfoPO;
import cn.com.zero.front.auth.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author liuxh
 * @version 1.0.0
 * @Description 用户接口
 * @createTime 2023/4/3 16:17
 */
@Service
public class UserService extends BaseService implements IUserService {

    private IUserMapper userMapper;
    private IFileService fileService;
    private FrontLockedUserService frontLockedUserService;

    @Override
    public AuthUserInfoPO getAuthUserInfoPO(String userId) {
        return userMapper.getAuthUserInfoById(userId);
    }

    @Override
    public String updateAvatar(String userId, MultipartFile avatar) {
        FileInfo fileInfo = fileService.upload(avatar, FrontAuthConstants.RELATIVE_PATH_AVATAR);
        String relativeFilePath = fileInfo.getRelativeFilePath();

        UserInfoPO modifyUserInfo = new UserInfoPO();
        modifyUserInfo.setUserId(userId);
        modifyUserInfo.setAvatar(relativeFilePath);
        userMapper.modifyUserInfo(modifyUserInfo);

        return relativeFilePath;
    }

    @Override
    public void updateUserName(String userId, String userName) {
        UserInfoPO modifyUserInfo = new UserInfoPO();
        modifyUserInfo.setUserId(userId);
        modifyUserInfo.setUserName(userName);
        userMapper.modifyUserInfo(modifyUserInfo);
    }

    @Override
    public void modifyMobile(String userId, String mobilePhone) {
        UserInfoPO modifyUserInfo = new UserInfoPO();
        modifyUserInfo.setUserId(userId);
        modifyUserInfo.setMobile(SensitiveInfoUtil.encryptMobile(mobilePhone));
        modifyUserInfo.setShowMobile(SensitiveInfoUtil.encodeMobile(mobilePhone));
        userMapper.modifyUserInfo(modifyUserInfo);
    }

    @Override
    public AuthUserPO queryUserByMobilePhone(String mobilePhone) {
        String encryptMobile = SensitiveInfoUtil.encryptMobile(mobilePhone);
        return userMapper.getUserByNameOrMobile(encryptMobile);
    }

    @Override
    public void resetLoginPwd(String userId, String password) {
        UserInfoPO modifyUserInfo = new UserInfoPO();
        modifyUserInfo.setUserId(userId);
        modifyUserInfo.setPassword(SensitiveInfoUtil.encryptPwd(password));
        userMapper.modifyUserInfo(modifyUserInfo);
    }

    @Override
    public void unlockCustomerByUserId(String userId) {
        // 解锁用户 清空错误登录次数
        frontLockedUserService.unlockUser(FrontAuthConstants.FRONT_SIDE_NAME, userId);
    }

    @Override
    public void applyCancellation(String phone, String userId) {
        String showPhone = "";
        String encryptPhone = "";
        if (StringUtils.hasLength(phone)) {
            showPhone = SensitiveInfoUtil.encodeMobile(phone);
            encryptPhone = SensitiveInfoUtil.encryptMobile(phone);
        }
        // 判断是否已经有待审核或审核成功的的注销申请的记录
        int applyRows = userMapper.queryUserCancellationApply(userId);
        if (applyRows > 0) {
            logger.warn("用户[{}]已存在待审核或审核通过的注销申请！", userId);
            throw new ErrorCodeException(-20009L);// 申请已存在
        }

        // 添加注销申请信息
        userMapper.insertCancelApply(userId, encryptPhone, showPhone);
    }

    @Autowired
    public void setUserMapper(IUserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Resource
    public void setFileService(IFileService fileService) {
        this.fileService = fileService;
    }

    @Autowired
    public void setFrontLockedUserService(FrontLockedUserService frontLockedUserService) {
        this.frontLockedUserService = frontLockedUserService;
    }
}
